package com.AA.config;
/**
*
* @author houfukude
*/
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.NodeList;

public class ConfigXML {

	@SuppressWarnings("rawtypes")
	private Hashtable hash;
	@SuppressWarnings("rawtypes")
	public void setHashtable( Hashtable hash){
		this.hash = hash;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Hashtable get(String xmlName,String itemName){
		//String cfgString[] = null;
		//数组方式已经过时。直接使用hash表提高效率
		hash = new Hashtable();
		String word = null;
		try {
			DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();  //先获取产生DocumentBuilder工厂,的工厂,在通过这个工厂产生一个DocumentBuilder,    
		    DocumentBuilder db=dbf.newDocumentBuilder();   //DocumentBuilder就是用来产生Document的  
		    org.w3c.dom.Document doc = db.parse(xmlName);  //这个Document就是一个XML文件在内存中的镜像   
			NodeList nodeList=doc.getElementsByTagName(itemName);//不同item的链表
			//cfgString = new String[nodeList.getLength()];//创建对应长度的数组
			for (int i = 0; i < nodeList.getLength(); i++) {
				//cfgString[i] = nodeList.item(i).getFirstChild().getNodeValue().trim();
				word = nodeList.item(i).getFirstChild().getNodeValue().trim();
				//System.out.println("[i]读取: [KEY: " + i +"][VALUE: " + word + "]");
				 this.hash.put(Integer.toString(i), word);
			}	
		}catch (Exception e) {
			e.printStackTrace();//throw new Exception("[e]:con't open " + xmlName + "  file not fond");
		}
  		return hash;

	}
}
